package com.bingo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bingo.common.R;
import com.bingo.dao.MeteorologyMapper;
import com.bingo.entity.CurPool;
import com.bingo.entity.Medication;
import com.bingo.entity.Meteorology;
import com.bingo.entity.dto.resp.MeteorologyRecord;
import com.bingo.entity.dto.resp.ReportBasic;
import com.bingo.service.CurPoolService;
import com.bingo.service.MeteorologyService;
import com.bingo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

@Service
public class MeteorologyServiceImpl extends ServiceImpl<MeteorologyMapper, Meteorology> implements MeteorologyService {
    private CurPoolService curPoolService;
    @Autowired
    public void setPrePoolService(CurPoolService curPoolService) {
        this.curPoolService = curPoolService;
    }

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    // 新增气象记录
    public R add(Meteorology meteorology) {
        // 查询该塘
        CurPool pool = curPoolService.getCurPoolByNo(meteorology.getPoolNo());

        if (pool==null){
            return R.error("该塘不存在！");
        }
        if (pool.getType()==0){
            return R.error("该塘是空塘！");
        }
        // 查询是否已经存在当天气象记录
        QueryWrapper<Meteorology> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pool_no", meteorology.getPoolNo());
        queryWrapper.eq("date", meteorology.getDate());
        Meteorology old = this.getOne(queryWrapper);

        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 填充数据
        Meteorology new_meteorology = new Meteorology();
        new_meteorology.setUserId(meteorology.getUserId());
        new_meteorology.setPoolNo(meteorology.getPoolNo());
        new_meteorology.setTemperature(meteorology.getTemperature());
        new_meteorology.setWind(meteorology.getWind());
        new_meteorology.setScale(meteorology.getScale());
        new_meteorology.setWeather(meteorology.getWeather());
        new_meteorology.setDate(meteorology.getDate());
        new_meteorology.setReportDate(Date.valueOf(today));

        // 还未存在当天气象记录
        if (old == null) {
            // 保存
            this.save(new_meteorology);
            return R.success("添加成功");
        } else {
            return R.error("存在当天记录！");
        }
    }
    public List<MeteorologyRecord> selectMeteorologyRecord(List<String> poolNos, Date startTime, Date endTime, String userId) {
        if (poolNos == null || poolNos.size() == 0){
            poolNos = curPoolService.getPoolNos();
        }

        QueryWrapper<Meteorology> wrapper = new QueryWrapper<>();
        wrapper.in("pool_no", poolNos);
        wrapper.between("date", startTime, endTime);
        if (userId != null && !userId.equals("")){
            wrapper.eq("user_id", userId);
        }
        List<Meteorology> list = this.list(wrapper);
        LinkedList<MeteorologyRecord> meteorologyRecords = new LinkedList<>();
        HashMap<String, String> allUser = userService.getAllUser();

        for (Meteorology meteorology : list) {
            MeteorologyRecord meteorologyRecord = new MeteorologyRecord();
            ReportBasic reportBasic = new ReportBasic();
            reportBasic.setPoolNo(meteorology.getPoolNo());
            String s = allUser.get(meteorology.getUserId());
            reportBasic.setReporter(s);
            reportBasic.setDate(meteorology.getDate());
            reportBasic.setReportDate(meteorology.getReportDate());
            meteorologyRecord.setTemperature(meteorology.getTemperature());
            meteorologyRecord.setWind(meteorology.getWind());
            meteorologyRecord.setScale(meteorology.getScale());
            meteorologyRecord.setWeather(meteorology.getWeather());
            meteorologyRecord.setReportBasic(reportBasic);
            meteorologyRecords.add(meteorologyRecord);
        }
        return meteorologyRecords;
    }

    @Override
    public boolean deleteMeteorologyRecord(Meteorology meteorology) {
        if (meteorology==null || meteorology.getPoolNo()==null || meteorology.getDate()==null ){
            return false;
        }
        QueryWrapper<Meteorology> wrapper = new QueryWrapper<>();
        wrapper.eq("pool_no", meteorology.getPoolNo());
        wrapper.eq("date", meteorology.getDate());
        // 调用remove方法来删除记录
        return this.remove(wrapper);

    }

    @Override
    public boolean update(Meteorology meteorology) {
        // 创建更新条件
        UpdateWrapper<Meteorology> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("pool_no", meteorology.getPoolNo());
        updateWrapper.eq("date", meteorology.getDate());

        // 设置需要更新的字段
        updateWrapper.set("temperature", meteorology.getTemperature());
        updateWrapper.set("wind", meteorology.getWind());
        updateWrapper.set("scale", meteorology.getScale());
        updateWrapper.set("weather", meteorology.getWeather());

        // 执行更新操作
        return this.update(updateWrapper);
    }

}





